package com.ywq.app.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ywq.app.dao.MemberDao;
import com.ywq.app.domain.ArtActivity;
import com.ywq.app.domain.ArtActivityMember;
import com.ywq.app.domain.ArtBack;
import com.ywq.app.domain.ArtOrders;
import com.ywq.app.domain.ArtProcessLog;
import com.ywq.app.domain.Member;
import com.ywq.app.domain.MemberAddress;
import com.ywq.app.domain.MemberBank;
import com.ywq.app.domain.MemberHas;
import com.ywq.app.domain.MemberHobby;
import com.ywq.app.domain.MemberSecurity;
import com.ywq.common.exception.AppBusinessException;

public class MemberDaoImpl extends SqlSessionDaoSupport implements MemberDao{
	
	private static final Logger logger = LoggerFactory.getLogger(MemberDaoImpl.class);
	
	 /**
	  * 
	  */
	@Override
	public void insert(Member member)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insert",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
	}
	
	
	 /**
	  * 
	  */
	@Override
	public void update(Member member)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.update",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}
	@Override
	public void updateSecurityMoney(Member member)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.updateSecurityMoney",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateSecurityMoney database failure.", e);
			}
			throw new AppBusinessException("updateSecurityMoney database failure.", e);
		}
	}
	//
	@Override
	public void updateMemberEnabled(String id)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.updateMemberEnabled",id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}
	
	@Override
	public void updateResetAccount(Map<String, Object> map)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.updateResetAccount",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateResetAccount database failure.", e);
			}
			throw new AppBusinessException("updateResetAccount database failure.", e);
		}
	}
	/**
	 * 
	 */
	@Override
	public Member getMemberByMap(Map<String, Object> map) throws AppBusinessException{
		
		try {
			Member me= (Member) getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getMemberByMap",map);
			return me;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberByMap failure.", e);
			}
			throw new AppBusinessException("getMemberByMap failure.", e);
		}
	}
	/**
	 * 得到订单
	 * @param map
	 * @return
	 * @throws AppBusinessException
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtOrders> getOrdersListByMemberId(Map<String, Object> map) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getOrdersListByMemberId",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrdersListByMemberId list failure.", e);
			}
			throw new AppBusinessException("getOrdersListByMemberId list failure.", e);
		}
	}
	
	@Override
	public int getOrdersByIdCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getOrdersByIdCount",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrdersByIdCount  failure.", e);
			}
			throw new AppBusinessException("getOrdersByIdCount  failure.", e);
		}
	}
	/**
	 * 活动
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtActivityMember> getActivityByMemberId(Map<String, Object> map) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getActivityListByMemberId",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getActivityByIdMemberId list failure.", e);
			}
			throw new AppBusinessException("getActivityByIdMemberId list failure.", e);
		}
	}
	//活动数
	@Override
	public int getActivityByIdCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getActivityByIdCount",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrdersByIdCount  failure.", e);
			}
			throw new AppBusinessException("getOrdersByIdCount  failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberHas> getHasList(Map<String, Object> map) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getHasList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getActivityByIdMemberId list failure.", e);
			}
			throw new AppBusinessException("getActivityByIdMemberId list failure.", e);
		}
	}
	
	//Has
	@Override
	public void insertMemberHas(MemberHas memberHas)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insertMemberHas",
					memberHas);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberHas database failure.", e);
			}
			throw new AppBusinessException("insertMemberHas database failure.", e);
		}
	}
	//noHas
	@Override
    public void deleteMemberHas(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("com.ywq.app.persistence.MemberMapper.deleteMemberHas", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMemberHas database failure.", e);
            }
            throw new AppBusinessException("deleteMemberHas database failure.", e);
        }
    }
	//不参加活动
	@Override
    public void noJoinActivity(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("com.ywq.app.persistence.MemberMapper.noJoinActivity", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("noJoinActivity database failure.", e);
            }
            throw new AppBusinessException("noJoinActivity database failure.", e);
        }
    }
	/**
	 * 银行账号默认
	 */
	
	public void updateBankDefault(MemberBank memberBank)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.updateMemberBank",
					memberBank);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateMemberBank database failure.", e);
			}
			throw new AppBusinessException("updateMemberBank database failure.", e);
		}
	}
	
	public void noAllBankDefault(String  memberId)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.noAllBankDefault",memberId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("noAllBankDefault database failure.", e);
			}
			throw new AppBusinessException("noAllBankDefault database failure.", e);
		}
	}
	
	/**
	 * 添加银行账号
	 */
	@Override
	public void insertMemberBank(MemberBank memberBank)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insertMemberBank",
					memberBank);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberBank database failure.", e);
			}
			throw new AppBusinessException("insertMemberBank database failure.", e);
		}
	}
	/**
	 * 删除银行账号
	 * 
	 */
	@Override
	public void deleteMemberBank(String bankId)throws AppBusinessException {
		
		try { 
			getSqlSession().delete("com.ywq.app.persistence.MemberMapper.deleteMemberBank",
					bankId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMemberBank database failure.", e);
			}
			throw new AppBusinessException("deleteMemberBank database failure.", e);
		}
	}
	/**
	 * 银行列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberBank> getBankListByMemberId(String memberId) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getBankListByMemberId",memberId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBankLiatByMemberId list failure.", e);
			}
			throw new AppBusinessException("getBankLiatByMemberId list failure.", e);
		}
	}
	//新增地址
	@Override
	public void insertMemberAddress(MemberAddress memberAddress)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insertMemberAddress",
					memberAddress);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberAddress database failure.", e);
			}
			throw new AppBusinessException("insertMemberAddress database failure.", e);
		}
	}
	@Override
	public void insertMemberHobby(MemberHobby memberHobby)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insertMemberHobby",
					memberHobby);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberHobby database failure.", e);
			}
			throw new AppBusinessException("insertMemberHobby database failure.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberAddress> getAddressListByMemberId(String memberId) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getAddressListByMemberId",memberId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAddressListByMemberId list failure.", e);
			}
			throw new AppBusinessException("getAddressListByMemberId list failure.", e);
		}
	}
	/*查询地址详情*/
	@Override
	public MemberAddress getAddressByaId(String id) throws AppBusinessException{
		
		try {
			MemberAddress me= (MemberAddress) getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getAddressByaId",id);
			return me;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAddressByaId failure.", e);
			}
			throw new AppBusinessException("getAddressByaId failure.", e);
		}
	}
	
	//更新地址
	@Override
	public void updateMemberAddress(MemberAddress memberAddress)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.updateMemberAddress",
					memberAddress);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberAddress database failure.", e);
			}
			throw new AppBusinessException("insertMemberAddress database failure.", e);
		}
	}
	//查询某一订单
	@Override
	public ArtOrders getOrdersById(String id) throws AppBusinessException{
		
		try {
			ArtOrders me= (ArtOrders) getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getOrdersById",id);
			return me;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrdersById failure.", e);
			}
			throw new AppBusinessException("getOrdersById failure.", e);
		}
	}
	//
	@Override
	public void updateOrders(Map<String, Object> map)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.updateOrders",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateOrders database failure.", e);
			}
			throw new AppBusinessException("updateOrders database failure.", e);
		}
	}
	//付款成功
	@Override
	public void payAlipaySuc(Map<String, Object> map)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.payAlipaySuc",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("payAlipaySuc database failure.", e);
			}
			throw new AppBusinessException("payAlipaySuc database failure.", e);
		}
	}
	
	@Override
	public int getMemberModeCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getMemberModeCount",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberModeCount  failure.", e);
			}
			throw new AppBusinessException("getMemberModeCount  failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Member> getMemberMode(Map<String, Object> map) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getMemberMode",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberMode list failure.", e);
			}
			throw new AppBusinessException("getMemberMode list failure.", e);
		}
	}
	
	@Override
	public void deleteMemberHobby(String memberId)throws AppBusinessException {
		
		try { 
			getSqlSession().delete("com.ywq.app.persistence.MemberMapper.deleteMemberHobby",memberId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMemberHobby database failure.", e);
			}
			throw new AppBusinessException("deleteMemberHobby database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberHobby> getHobbyListByMemberId(String memberId) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getHobbyListByMemberId",memberId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getHobbyListByMemberId list failure.", e);
			}
			throw new AppBusinessException("getHobbyListByMemberId list failure.", e);
		}
	}
	@Override
	public void deleteComment(String commentId)throws AppBusinessException {
		
		try { 
			getSqlSession().delete("com.ywq.app.persistence.MemberMapper.deleteComment",commentId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteComment database failure.", e);
			}
			throw new AppBusinessException("deleteComment database failure.", e);
		}
	}
	
	@Override
	public int getCountThity(String memberId)throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getCountThity",memberId);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getCountThity  failure.", e);
			}
			throw new AppBusinessException("getCountThity  failure.", e);
		}
	}
	//添加保证金
	@Override
	public void insertMemberSecurity(MemberSecurity memberSecurity)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insertMemberSecurity",
					memberSecurity);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberSecurity database failure.", e);
			}
			throw new AppBusinessException("insertMemberSecurity database failure.", e);
		}
	}
	//退货
	@Override
	public void insertArtBack(ArtBack artBack)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insertArtBack",
					artBack);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertArtBack database failure.", e);
			}
			throw new AppBusinessException("insertArtBack database failure.", e);
		}
	}
	@Override
	public void updateArtBack(ArtBack artBack)throws AppBusinessException {
		
		try { 
			getSqlSession().update("com.ywq.app.persistence.MemberMapper.updateArtBack",
					artBack);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateArtBack database failure.", e);
			}
			throw new AppBusinessException("updateArtBack database failure.", e);
		}
	}
	
	//退货日志
	@Override
	public void insertArtProcessLog(ArtProcessLog artProcessLog)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("com.ywq.app.persistence.MemberMapper.insertArtProcessLog",
					artProcessLog);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertArtProcessLog database failure.", e);
			}
			throw new AppBusinessException("insertArtProcessLog database failure.", e);
		}
	}
	//查询保证金纪录
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberSecurity> getMemberSecurity(Map<String, Object> map) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getMemberSecurity",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberSecurity list failure.", e);
			}
			throw new AppBusinessException("getMemberSecurity list failure.", e);
		}
	}
	@Override
	public int getMemberSecurityCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getMemberSecurityCount",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberSecurityCount  failure.", e);
			}
			throw new AppBusinessException("getMemberSecurityCount  failure.", e);
		}
	}
	
	@Override
	public MemberSecurity getMemberSecurityById(Map<String, Object> map) throws AppBusinessException{
		
		try {
			MemberSecurity me= (MemberSecurity) getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getMemberSecurityById",map);
			return me;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberSecurityById failure.", e);
			}
			throw new AppBusinessException("getMemberSecurityById failure.", e);
		}
	}
	@Override
	public ArtBack getArtBackByOid(Map<String, Object> map) throws AppBusinessException{
		
		try {
			ArtBack me= (ArtBack) getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getArtBackByOid",map);
			return me;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtBackByOid failure.", e);
			}
			throw new AppBusinessException("getArtBackByOid failure.", e);
		}
	}
	
	//退回总数
	@Override
	public int memberBackListCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.memberBackListCount",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("memberBackListCount  failure.", e);
			}
			throw new AppBusinessException("memberBackListCount  failure.", e);
		}
	}
	//退货列表
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtOrders> memberBackListGrid(Map<String, Object> map) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.memberBackListGrid",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("memberBackListGrid list failure.", e);
			}
			throw new AppBusinessException("memberBackListGrid list failure.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ArtProcessLog> getProcessList(Map<String, Object> map) throws AppBusinessException {

		try {
			return getSqlSession().selectList( "com.ywq.app.persistence.MemberMapper.getProcessList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getProcessList list failure.", e);
			}
			throw new AppBusinessException("getProcessList list failure.", e);
		}
	}
	@Override
	public ArtProcessLog getArtProcessLogByRef(Map<String, Object> map) throws AppBusinessException{
		
		try {
			ArtProcessLog me= (ArtProcessLog) getSqlSession().selectOne("com.ywq.app.persistence.MemberMapper.getProcessList",map);
			return me;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtProcessLogByRef failure.", e);
			}
			throw new AppBusinessException("getArtProcessLogByRef failure.", e);
		}
	}
	
}
